class Solution
{
private:
    int dfs(int i, int j, vector<vector<int>>& grid)
    {
        if (!(0 <= i && i < grid.size() && 0 <= j && j < grid[0].size()) || grid[i][j] != 1)
            return 0;

        grid[i][j] = 2;
        return 1
            + dfs(i - 1, j, grid)
            + dfs(i + 1, j, grid)
            + dfs(i, j - 1, grid)
            + dfs(i, j + 1, grid);
    }

public:
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        if (grid.empty() || grid[0].empty())
            return 0;

        int ans = 0;
        for (int i = 0; i < grid.size(); ++i)
            for (int j = 0; j < grid[0].size(); ++j)
                if (grid[i][j] == 1)
                    ans = max(ans, dfs(i, j, grid));

        return ans;
    }
};